jQuery(function($) {
	
	$('#btn-login-dark').on('click', function(e) {
		$('body').attr('class', 'login-layout');
		$('#id-text2').attr('class', 'white');
		$('#id-company-text').attr('class', 'blue');

		e.preventDefault();
	});
	$('#btn-login-light').on('click', function(e) {
		$('body').attr('class', 'login-layout light-login');
		$('#id-text2').attr('class', 'grey');
		$('#id-company-text').attr('class', 'blue');

		e.preventDefault();
	});
	$('#btn-login-blur').on('click', function(e) {
		$('body').attr('class', 'login-layout blur-login');
		$('#id-text2').attr('class', 'white');
		$('#id-company-text').attr('class', 'light-blue');

		e.preventDefault();
	});

	//重新加载验证码
	function reloadVerifyImg(){
		var $img = $("#verify-img");
		var verifyImgUrl = $img.attr("src");
		var rnd = Math.random();
		$img.attr("src",verifyImgUrl + "?rnd=" + rnd);
	}
	
	$("#verify-img").click(reloadVerifyImg);
	var available = false;
	
	//校验验证码
	$("#verify-code-input").keyup(function(e){
		var $this = $(this);
		var code = $this.val();
		var msg = "";
		
		if(code.length == 6 ){
			$.ajax({
				dataType: "json",
				url: globalURL +"/admin/validateCaptchaCode",
				data: "vCode=" + code,
				async: false,
				success: function(_msg){
					if(_msg.validate){
						available = true;
						$("#verify-icon").removeClass("glyphicon-remove").removeClass("glyphicon-remove").addClass("glyphicon-ok").css("visibility","visible");
						if(e.keyCode == 13){
							$("#loginBtn").trigger("click");
						}
					}else{
						msg = "校验码输入错误";
						$("#verify-icon").removeClass("glyphicon-ok").addClass("glyphicon-remove").css("visibility","visible");
						$('#_msg').addClass('alert-block').css("color","red").html(msg);
					}
					
				},
				error: function(_msg){
					msg = "服务器异常， 请稍候再试!";
					$('#_msg').addClass('alert-block').html(msg);
				}
			});
		}else{
			$("#verify-icon").css("visibility","hidden");
			available = false;
		}
		
	});
	
	function showErr(msg){
		$('#_msg').addClass('alert alert-block alert-danger').html(msg);
	}
	
	//登录
	$("#loginBtn").click(function(){
		
		$("#passwd").val(CryptoJS.MD5(passwd+"{"+username+"}"));
		alert(CryptoJS.MD5(passwd+"{"+username+"}"));
		$("#loginForm").submit();
		/*
		
		var msg = '';
		
		var username = $.trim($("#username").val());
		if(username.length == 0) {
			msg += '用户名不能为空<br/>';
		}
		
		var passwd = $.trim( $("#passwd").val() );
		if( passwd.length == 0 ){
			msg +=   "密码不能为空<br/>";
			showErr(msg);
		}
		
		var vCode = $.trim($("#verify-code-input").val());
		if(vCode.length == 0){
			msg +=   "验证码不能为空<br/>";
		}
		
		if(vCode.length>0 && !available){
			msg +=   "验证码错误<br/>";
		}
		
		if(msg.length > 0) {
			showErr(msg);
		} else {
//			alert(CryptoJS.MD5(passwd+"{"+username+"}"));
			$("#passwd").val(CryptoJS.MD5(passwd+"{"+username+"}"));
			$("#loginForm").submit();
		}
		*/
	});
	
	$('#regBtn').click(function(e){
		var msg = "";
		var _username = $('#username').val();
		if(_username=='') {
			msg += '用户名不能为空<br/>';
		} else {
			$.ajax({
				dataType: "json",
				url: globalURL + "/admin/exists",
				data: "key=" + _username,
				async: false,
				success: function(_msg){
					if(_msg.exist){
						msg +=  (_username + "已存在!<br/>");
					}
				}
			});
		}
		if($('#passwd').val()=='') {
			msg += '密码不能为空<br/>';
		}
		if(!$("#accept").is(':checked')) {
			msg += '请接受用户协议';
		}
		$('#_msg').addClass('alert alert-block alert-danger').html(msg);
		if(msg == '') {
			$('#regist').submit();
		}
	});
	
});